using System;
using System.Collections.Generic;
using System.Text;
using DslFactory.Utilities.DteCodeModel;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Modeling;
using System.Net.Security;
using System.IO;
using VSLangProj;

namespace Dslfactory.wcfLanguage
{
 public static class IncrementalCodeGeneration
 {
  /// <summary>
  /// Subscribes to certain Store change events in order to direct the incremental code geenration when some model elements
  /// are added / removed or changed
  /// </summary>
  /// <param name="Store">Store events of which we want to subscribe to</param>
  internal static void SubsribeToStoreEvents(Store Store)
  {
   EventManagerDirectory eventManagerDirectory = Store.EventManagerDirectory;

   DataClassIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
   InterfaceIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
   DataContractIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
   MessageContractIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
   ServiceIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
   ServiceHostIncrementalCodeGeneration.SubsribeToStoreEvents(Store);
  }

  /// <summary>
  /// Generate the class or interface corresponding to the TypeOfInterest
  /// </summary>
  /// <param name="type"></param>
  public static void Generate(TypeOfInterest type)
  {
   // Get the project
   Project project = StoreCorrespondance.GetProjectForStore(type.Store);
   if (project == null)
    return;

   // Ensures a source file
   ProjectItem sourceFile = IncrementalGenerator.EnsureSourcefile(project, type.Name+".cs");

   // Add the code namespace
   CodeNamespace ns = IncrementalGenerator.EnsureNamespace(sourceFile, type.Namespace);

   // And ensure the interface
   if (type is Interface)
    InterfaceIncrementalCodeGeneration.Generate(type, ns);
   else if (type is MessageContract)
    MessageContractIncrementalCodeGeneration.Generate(type, ns);
   else if (type is DataContract)
    DataContractIncrementalCodeGeneration.Generate(type, ns);
   else if (type is Service)
    ServiceIncrementalCodeGeneration.Generate(type, ns);
   else if (type is Hosting)
    ServiceHostIncrementalCodeGeneration.Generate(type, ns);
  }
 }
}
